﻿using CfgTable;
using IQIGame.Onigao.Framework;

namespace IQIGame.Onigao.GamePlay
{
    public class SkillBaseData : IReusableClass
    {
        public int cid { get; private set; }
        /// <summary>
        /// 等级从1级开始
        /// </summary>
        public int lv { get; private set; }
        public bool isUnlock { get; private set; } = false;

        #region 配置 && 逻辑
        private CfgSkill _cfgData;
        public CfgSkill cfgData
        {
            get
            {
                if (this._cfgData == null && this.cid > 0)
                {
                    this._cfgData = TableCenter.skill.Get(cid);
                }
                return _cfgData;
            }
        }

        private CfgSkillLevel _cfgLv;
        public CfgSkillLevel cfgLv
        {
            get
            {
                if (this.lv > 0 && (this._cfgLv == null || this._cfgLv.SkillLevel != this.lv))
                {
                    this._cfgLv = TableCenter.skillLevel.GetCfg(this.cid, this.lv);
                }
                return this._cfgLv;
            }
        }
        public CfgSkillLevel cfgNextLv => TableCenter.skillLevel.GetCfg(this.cid, this.lv + 1);

        public CfgSkillLevel GetCfgSkillLevel(int lv)
        {
            return TableCenter.skillLevel.GetCfg(this.cid, lv);
        }

        public int maxLv
        {
            get
            {
                int maxLv = 0;
                var cfg = TableCenter.skillLevel.MaxCfg(this.cid);
                if (cfg != null)
                    maxLv = cfg.SkillLevel;
                return maxLv;
            }
        }

        public bool isMaxLv => this.lv >= this.maxLv;

        public void UpLv()
        {
            this.lv++;
        }
        #endregion

        public SkillBaseData() { }

        public SkillBaseData Sync(int skillCid, int skillLev, bool isUnlock = true)
        {
            this.cid = skillCid;
            this.lv = skillLev;
            this.isUnlock = isUnlock;
            return this;
        }

        public void Clear()
        {
            this.cid = 0;
            this.lv = 0;
            this.isUnlock = false;
            this.ClearLogicData();
            this.OnClear();
        }

        private void ClearLogicData()
        {
            this._cfgData = null;
            this._cfgLv = null;
        }

        protected virtual void OnClear() { }

        #region 对象池
        public virtual uint MaxStore => 10;

        public void OnReset()
        {
            this.Clear();
        }

        public void Put2Pool()
        {
            ClassPool.PutAutoType(this);
        }

        static public SkillBaseData NewFromPool()
        {
            return ClassPool<SkillBaseData>.Get();
        }
        #endregion

    }
}
